package com.xyht.sca_s.student_manage_system.modules.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.SmsVisitorProcess;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.dto.SmsVisitorProcessDTO;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.vo.SmsVisitorProcessVO;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.vo.SmsVisitorRegisterVO;
import com.xyht.sca_s.student_manage_system.modules.visitor.mapper.SmsVisitorProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.visitor.service.SmsVisitorProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.visitor.contact.CheckType.PROCESS_PEOPLE_CHECK_TYPE;
import static com.xyht.sca_s.student_manage_system.modules.visitor.contact.CheckType.PROCESS_ROLE_CHECK_TYPE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-04-18
 */
@Service
public class SmsVisitorProcessServiceImpl extends ServiceImpl<SmsVisitorProcessMapper, SmsVisitorProcess> implements SmsVisitorProcessService {
    @Autowired
    SmsVisitorProcessMapper smsVisitorProcessMapper;
    @Autowired
    SmsUserMapper smsUserMapper;
    @Autowired
    SmsRoleMapper smsRoleMapper;

    @Transactional
    @Override
    public ResponseResult smsVisitorProcessAdd(SmsVisitorProcessVO smsVisitorProcessVO, SmsVisitorRegisterVO smsVisitorRegisterVO) {
        if(isNullOrEmpty(smsVisitorProcessVO.getCheckType())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //判断设置的checkId是否存在

        if(smsVisitorProcessVO.getCheckType()==PROCESS_PEOPLE_CHECK_TYPE){//如果传入的是个人：0
            QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsUser::getId,smsVisitorRegisterVO.getInterviewerId());
            SmsUser query = smsUserMapper.selectOne(queryWrapper);
            if(isNullOrEmpty(query)){
                return CommonResult.failed(CommonCodeEnum.PROCESS_PERSON_NOT_EXIST);
            }
            SmsVisitorProcess smsVisitorProcess = new SmsVisitorProcess();
            Integer sortCount = smsVisitorProcessMapper.selectCount(new QueryWrapper<>());
            Integer newSortCount = sortCount+1;
            smsVisitorProcess.setUserName(query.getRealName());
            smsVisitorProcess.setCheckId(smsVisitorProcessVO.getCheckId());
            smsVisitorProcess.setCheckType(smsVisitorProcessVO.getCheckType());
            smsVisitorProcess.setSort(newSortCount);
            smsVisitorProcessMapper.insert(smsVisitorProcess);
        }
        if(smsVisitorProcessVO.getCheckType()==PROCESS_ROLE_CHECK_TYPE){//如果设置的类型是角色：1
            QueryWrapper<SmsRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsRole::getId,smsVisitorProcessVO.getCheckId());
            SmsRole query = smsRoleMapper.selectOne(queryWrapper);
            if(isNullOrEmpty(query)){
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.PROCESS_ROLE_NOT_EXIST));
            }
            SmsVisitorProcess smsVisitorProcess = new SmsVisitorProcess();
            Integer sortCount = smsVisitorProcessMapper.selectCount(new QueryWrapper<>());
            smsVisitorProcess.setRoleName(query.getRoleName());
            Integer newSortCount = sortCount+1;
            smsVisitorProcess.setCheckId(smsVisitorProcessVO.getCheckId());
            smsVisitorProcess.setCheckType(smsVisitorProcessVO.getCheckType());
            smsVisitorProcess.setSort(newSortCount);
            smsVisitorProcessMapper.insert(smsVisitorProcess);
        }



        return CommonResult.success();
    }

    @Transactional
    @Override
    public ResponseResult deleteSmsVisitorProcess(SmsVisitorProcessVO smsVisitorProcessVO) {
        if(isNullOrEmpty(smsVisitorProcessVO.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVisitorProcess query = smsVisitorProcessMapper.selectById(smsVisitorProcessVO.getId());
        if(isNullOrEmpty(query)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        Integer sortCount = smsVisitorProcessMapper.selectCount(new QueryWrapper<>());
        if(query.getSort().equals(sortCount)){
            smsVisitorProcessMapper.deleteById(smsVisitorProcessVO.getId());
        }
        else {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.VISITOR_NOT_LAST_ONE));
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateSmsVisitorProcess(SmsVisitorProcessVO smsVisitorProcessVO) {
          if(isNullOrEmpty(smsVisitorProcessVO.getId())||isNullOrEmpty(smsVisitorProcessVO.getCheckId())||
          isNullOrEmpty(smsVisitorProcessVO.getCheckType())){
              return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
          }
        SmsVisitorProcess query = smsVisitorProcessMapper.selectById(smsVisitorProcessVO.getId());
        if(query==null){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        if(smsVisitorProcessVO.getCheckType()==PROCESS_PEOPLE_CHECK_TYPE){//如果传入的是个人：0
            QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsUser::getId,smsVisitorProcessVO.getCheckId());
            SmsUser query1 = smsUserMapper.selectOne(queryWrapper);
            if(isNullOrEmpty(query1)){
                return CommonResult.failed(CommonCodeEnum.PROCESS_PERSON_NOT_EXIST);
            }
        }
        if(smsVisitorProcessVO.getCheckType()==PROCESS_ROLE_CHECK_TYPE){//如果修改的类型是角色：1
            QueryWrapper<SmsRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsRole::getId,smsVisitorProcessVO.getCheckId());
            SmsRole query1 = smsRoleMapper.selectOne(queryWrapper);
            if(isNullOrEmpty(query1)){
                return CommonResult.failed(CommonCodeEnum.PROCESS_ROLE_NOT_EXIST);
            }
        }
        SmsVisitorProcess smsVisitorProcess = new SmsVisitorProcess();
        BeanUtils.copyProperties(smsVisitorProcessVO, smsVisitorProcess);
        smsVisitorProcess.setSort(smsVisitorProcessVO.getSort());
        smsVisitorProcessMapper.updateById(smsVisitorProcess);
       return CommonResult.success();
    }





    @Override
    public ResponseResult selectSmsVisitorProcess() {
        QueryWrapper<SmsVisitorProcess> queryWrapper = new QueryWrapper<>();

        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        queryWrapper.orderByAsc("sort");

        List<SmsVisitorProcess> list1 = smsVisitorProcessMapper.selectList(queryWrapper);
        if (list1.size() == 0) {
            return CommonResult.success(list1);
        }

        return CommonResult.success(list1);
    }

    @Override
    public ResponseResult selectSmsVisitorProcessByProcessPeople(SmsVisitorProcessVO smsVisitorProcessVO) {
        return null;
    }

    @Override
    public ResponseResult selectSmsVisitorProcessById(String id) {
        if(isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVisitorProcess query = smsVisitorProcessMapper.selectById(id);
        if(query ==null|| query.getId()==null){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsVisitorProcess smsVisitorProcess = new SmsVisitorProcess();
        smsVisitorProcess.setId(id);
        SmsVisitorProcessDTO smsVisitorProcessDTO = new SmsVisitorProcessDTO();
        BeanUtils.copyProperties(query,smsVisitorProcessDTO);
        return CommonResult.success(smsVisitorProcessDTO);
    }
}
